package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserClient;
import com.tsd.system.entity.SysUserClientExt;
import com.tsd.system.service.SysUserClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserClientsys_user_client 控制层
 *
 * @author zsy
 * @date 2021/01/15 10:01:49
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "sys_user_client-sys_user_client", description = "sys_user_client的API描述")
public class SysUserClientController extends SystemBaseController {

    @Resource
    private SysUserClientService sysUserClientService;

    @ApiOperation(value = "根据id删除sys_user_client", notes = "根据id删除sys_user_client")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "sys_user_clientid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysUserClient.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysUserClient() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            sysUserClientService.deleteSysUserClient(id, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存sys_user_client", notes = "保存sys_user_client，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "sys_user_client", required = true, paramType = "body", dataType = "SysUserClient")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysUserClient.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysUserClient() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            SysUserClient t = JSON.parseObject(json, SysUserClient.class);
            sysUserClientService.saveSysUserClient(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询sys_user_client", notes = "分页查询sys_user_client")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysUserClients.action", method = RequestMethod.POST)
    public HttpResult<List<SysUserClientExt>> findSysUserClients() {
        HttpResult<List<SysUserClientExt>> hr = new HttpResult<List<SysUserClientExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "user_sid", "user_sid", false, false);
            HlpUtils.copyStringValue(map, params, "client_sid", "client_sid", false, false);
            PageBean page = new PageBean(map);
            if (map.containsKey("nopage")) {
                page = null;
            }
            List<SysUserClientExt> list;
            if (HlpUtils.isEmpty(page)) {
                list = sysUserClientService.findSysUserClients(params);
            } else {
                list = sysUserClientService.findSysUserClients(params, page);
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取用户拥有的客户")
    @ResponseBody
    @RequestMapping(value = "/findClient4User.action", method = RequestMethod.POST)
    public HttpResult<Object> findClient4User() {
        HttpResult<Object> hr = new HttpResult<Object>();
        try {
            SysUser opUser = this.getSysUser();
            Map<String, Object> map = this.getParams(request);
            String userId = HlpUtils.getString(map, "userId");
//            List<SysClientExt> list = sysUserClientService.findSysClient4User(userId);
//            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 设置客户的数据权限
     */
    @ResponseBody
    @RequestMapping(value = "/settingClientSysUser.action", method = RequestMethod.POST)
    public HttpResult<String> settingClientUser() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String clientSid = HlpUtils.getString(map, "client_sid");
            super.checkEmpty(clientSid, "请提供参数client_sid");
            Object obj = map.get("sys_user_list");
            if (obj instanceof List) {
                List<SysUserClientExt> list = JSONArray.parseArray(JSON.toJSONString(obj), SysUserClientExt.class);
                sysUserClientService.settingClientSysUserNew(clientSid, list, opUser);
            } else {
                throw new HlpException("sys_user_list非数组格式");
            }

            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
